/*
 * This page prepares the teacher for the manage teacher page.
 */
package controllers.classes;

import classes.Breadcrumbs;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Employee;
import dao.EmployeeDao;
import classes.HibernateUtil;
import dao.CourseDao;
import dao.RoleDao;
import models.Course;
import models.Role;

/**
 *
 * @author Achie
 */
public class TeacherController extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("id") != null) { //Edit teacher
            editTeacher(Long.parseLong(request.getParameter("id")), request, response);
        } else if (request.getParameter("idremove") != null) { //remove teacher
            removeTeacher(Long.parseLong(request.getParameter("idremove")), request, response);

        } else if (request.getParameter("search") != null) { //if search has been invoked
            String search = request.getParameter("search");
            if (request.getParameter("searchtype").equals(("name"))) { //search by name
                List<Employee> emplist = searchByName(search);
                request.setAttribute("employees", emplist);
                request.setAttribute("courses", getCourseList());
                redirect(request, response);
            } else if (request.getParameter("searchtype").equals("email")) { //search by email
                List<Employee> emplist = searchByEmail(search);
                request.setAttribute("employees", emplist);
                request.setAttribute("courses", getCourseList());
                redirect(request, response);
            } else { //search by course
                List<Employee> emplist = searchByCourse(search);
                request.setAttribute("employees", emplist);
                request.setAttribute("courses", getCourseList());
                redirect(request, response);
            }

        } else { //Get the teachers list and redirect to the teachers page
            getTeacherList(request);
            request.setAttribute("courses", getCourseList());
            redirect(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    public void editTeacher(long id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Get all the information about the teacher to fill in the form.
        HibernateUtil hu = new HibernateUtil();
        EmployeeDao ed = new EmployeeDao(hu.openSession());

        List<Employee> employeeList = ed.findAll();
        
        Employee temp = null;
        temp = ed.get(id); //get employee by id

        if (temp != null) { //if it is set
            request.setAttribute("teacheredit", temp);
        } 

        RoleDao rd = new RoleDao(hu.openSession());
        List<Role> rolelist = rd.findAll();
        request.setAttribute("roleList", rolelist);
        
        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Teacher", "Manage teachers"),
            new Breadcrumbs("Teacher/edit?id", "Edit teacher")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/add_edit_teachers.jsp");
        dispatcher.forward(request, response);
    }

    /*
     * this method will be invoked when pressed on delete teacher
     */
    public void removeTeacher(long id, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HibernateUtil hu = new HibernateUtil();
        EmployeeDao ed = new EmployeeDao(hu.openSession());

        ed.delete(ed.get(id));
        hu.close();

        response.sendRedirect("../Teacher");
    }

    /*
     * this method sets the attribute with all the employers
     */
    public void getTeacherList(HttpServletRequest request) {
        HibernateUtil hu = new HibernateUtil();
        EmployeeDao ed = new EmployeeDao(hu.openSession());

        List<Employee> employeeList = ed.findAll();

        request.setAttribute("employees", employeeList);
    }

    /*
     * this method gets all the courses
     */
    public List<Course> getCourseList() {
        HibernateUtil hu = new HibernateUtil();
        CourseDao cd = new CourseDao(hu.openSession());
        return cd.findAll();
    }

    /*
     * this method invokes search for a teacher by name
     */
    public List<Employee> searchByName(String search) {
        HibernateUtil hu = new HibernateUtil();
        EmployeeDao ed = new EmployeeDao(hu.openSession());
        List<Employee> teacherList = ed.findAllByName(search);
        return teacherList;
    }

    /*
     * this method invokes search for a teacher by email
     */
    public List<Employee> searchByEmail(String search) {
        HibernateUtil hu = new HibernateUtil();
        EmployeeDao ed = new EmployeeDao(hu.openSession());
        return ed.findAllByEmail(search);
    }

    /*
     * this method invokes search for (a) teacher(s) by course
     */
    public List<Employee> searchByCourse(String search) {
        HibernateUtil hu = new HibernateUtil();
        EmployeeDao ed = new EmployeeDao(hu.openSession());
        return ed.findAllByCourse(search);
    }

    public void redirect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Teacher", "Manage teachers")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/manage_teachers.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
